package com.mall4j.cloud.product.feign;


import com.mall4j.cloud.api.multishop.bo.EsShopDetailBO;
import com.mall4j.cloud.api.multishop.feign.ShopDetailFeignClient;
import com.mall4j.cloud.api.product.bo.EsProductBO;
import com.mall4j.cloud.api.product.feign.ProductFeignClient;
import com.mall4j.cloud.common.response.ServerResponseEntity;
import com.mall4j.cloud.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ProductFeignController implements ProductFeignClient {

    @Autowired
    private ProductService productService;

    @Autowired
    private ShopDetailFeignClient shopDetailFeignClient;


    @Override
    public ServerResponseEntity<EsProductBO> loadEsProductBO(Long spuId) {
        EsProductBO esProductBO = productService.getBySpuId(spuId);
        try {
            ServerResponseEntity<EsShopDetailBO> shopDetail = shopDetailFeignClient.getShopByShopId(esProductBO.getShopId());
            EsShopDetailBO data = shopDetail.getData();
            esProductBO.setShopName(data.getShopName());
            esProductBO.setShopImg(data.getShopLogo());
            esProductBO.setShopType(data.getType());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ServerResponseEntity.success(esProductBO);
    }

    @Override
    public ServerResponseEntity<List<Long>> getSpuIdsByShopCategoryIds(List<Long> shopCategoryIds) {
        return ServerResponseEntity.success(productService.getSpuIdsByShopCategoryIds(shopCategoryIds));
    }

    @Override
    public ServerResponseEntity<List<Long>> getSpuIdsByCategoryIds(List<Long> categoryIds) {
        return ServerResponseEntity.success(productService.getSpuIdsByCategoryIds(categoryIds));
    }

    @Override
    public ServerResponseEntity<List<Long>> getSpuIdsByBrandId(Long brandId) {
        return ServerResponseEntity.success(productService.getSpuIdsByBrandId(brandId));
    }

    @Override
    public ServerResponseEntity<List<Long>> getSpuIdsByShopId(Long shopId) {
        return ServerResponseEntity.success(productService.getSpuIdsByShopId(shopId));
    }
}
